package tr.edu.yildiz.dap.appeditor.pages.composites;

import java.util.List;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.AbstractFormPart;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import tr.edu.yildiz.ce.dap.appagent.model.Actions;
import tr.edu.yildiz.ce.dap.appagent.model.Component;
import tr.edu.yildiz.ce.dap.appagent.util.DeploymentConstants;
import tr.edu.yildiz.dap.appeditor.dialogs.componentdialog.ComponentDialog;
import tr.edu.yildiz.dap.appeditor.model.AppComponent;
import tr.edu.yildiz.dap.appeditor.pages.MainPage;

public class ComponentsComposite extends Composite {

	ListEditorComposite listComposite;
	MainPage mainPage;
	FormToolkit toolkit; 
	Component selectedAppComponent;
	
	Section listSection;
	Section detailSection;
	
	Text nameText;
	Text idText;
	Text versionText;
	Text locationText;
	Combo action;
	
	
	public ComponentsComposite(Composite parent,MainPage page , int style){
		super(parent, style);
		toolkit = new FormToolkit(this.getDisplay());
		toolkit.adapt(this);
		
		GridLayout layout = new GridLayout();
	    layout.marginWidth = 0;
	    layout.numColumns = 2;
	    layout.makeColumnsEqualWidth = true;
	    setLayout(layout);
		
		this.mainPage = page;
		IFormPart formPart = new AbstractFormPart() {};
		mainPage.getManagedForm().addPart(formPart);
		createGeneralComposite();
	    createListComposite();
	    createDetails();
	}
	
	
	private void createDetails(){
		
	    detailSection = toolkit.createSection(this,  ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED | ExpandableComposite.TWISTIE);
		detailSection.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, true));
		detailSection.setText("Details");
		
		Composite detailsComposite = toolkit.createComposite(detailSection, SWT.NONE);
		detailsComposite.setLayoutData(new GridData(SWT.BEGINNING, SWT.TOP, true, true));
		GridLayout gridLayout_1 = new GridLayout(4, true);
		detailsComposite.setLayout(gridLayout_1);
		
		Label nameLabel = toolkit.createLabel(detailsComposite, "Name : ", SWT.NONE);
		nameLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, true));
		
		nameText = toolkit.createText(detailsComposite,null, SWT.NONE);
		GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, true);
		gd.horizontalSpan = 3;
		nameText.setLayoutData(gd);
		nameText.setEnabled(false);
		
		Label idLabel = toolkit.createLabel(detailsComposite, "ID : ", SWT.NONE);
		idLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, true));
		
		idText = toolkit.createText(detailsComposite,null, SWT.NONE);
		idText.setLayoutData(gd);
		idText.setEnabled(false);
		
		Label versionLabel = toolkit.createLabel(detailsComposite, "Version : ", SWT.NONE);
		versionLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, true));
		
		versionText = toolkit.createText(detailsComposite,null, SWT.NONE);
		versionText.setLayoutData(gd);
		versionText.setEnabled(false);

		Label locationLabel = toolkit.createLabel(detailsComposite, "Location : ", SWT.NONE);
		locationLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, true));

		locationText = toolkit.createText(detailsComposite,null, SWT.NONE);
		locationText.setLayoutData(gd);
		locationText.setEnabled(false);
		
		Label actionLabel = toolkit.createLabel(detailsComposite, "Action : ", SWT.NONE);
		locationLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, true));

		action = new Combo(detailsComposite, SWT.NONE);
		action.setLayoutData(gd);
		action.add(DeploymentConstants.Action.INSTALL);
		action.add(DeploymentConstants.Action.START);
		
		action.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent selectionEvent) {
				String selectedValue =   action.getText();
				if (selectedValue.equals(DeploymentConstants.Action.INSTALL)){
					if (selectedAppComponent.getActions() != null)
						if (selectedAppComponent.getActions().getAction().contains(DeploymentConstants.Action.START)){
							selectedAppComponent.getActions().getAction().remove(DeploymentConstants.Action.START);
							markDirty();
						}
				}else if (selectedValue.equals(DeploymentConstants.Action.START)){
					if (selectedAppComponent.getActions() == null){
						selectedAppComponent.setActions(new Actions());
						markDirty();
					}
					if (!selectedAppComponent.getActions().getAction().contains(DeploymentConstants.Action.START)){
						selectedAppComponent.getActions().getAction().add(DeploymentConstants.Action.START);
						markDirty();						
					}
				}
					
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		detailSection.setClient(detailsComposite);
		toolkit.paintBordersFor(detailsComposite);
	}
	
	private void addAppComponent(List<Component> list,AppComponent appComponent ){
		if (!(list.contains(appComponent))){
			Component component = new Component();
			component.setName(appComponent.getId());
			component.setVersion(appComponent.getVersion());
			component.setUrl(appComponent.getUrlString());
			component.setActions(new Actions());
			component.getActions().getAction().add(DeploymentConstants.Action.INSTALL);
			list.add(component);
		}
		for (AppComponent depComponent : appComponent.getDependencyList()) {
			addAppComponent(list, depComponent);
		}
	}
	
	private void createListComposite(){
		
		listSection = toolkit.createSection(this,  ExpandableComposite.TITLE_BAR);
		listSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		listSection.setText("Dependencies");
		
		listComposite = new ListEditorComposite(listSection, SWT.NONE);
		listComposite.setContentProvider(new ListEditorContentProvider<Component>());
		listComposite.setInput(mainPage.getComponentList().getComponent());

		listComposite.setAddListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				AppComponent appComponent = null;
				ComponentDialog componentDailog = new ComponentDialog(
						getShell());
				if (componentDailog.open() == Window.OK) {
					appComponent = componentDailog.getSelectedAppComponent();
					if (appComponent == null)
						return;
					addAppComponent(listComposite.getInput(),appComponent);
					markDirty();
					listComposite.refresh();
					// ((ApplicationModelEditor)mainPage.getEditor()).refreshEditor();
				}
			}
		});
		
		listComposite.setRemoveListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						Component component = listComposite.getSelected();
						listComposite.getInput().remove(component);
						markDirty();
						listComposite.refresh();
						listComposite.removeButton.setEnabled(false);
//						((FileEditorInput)mainPage.getEditor()).refreshEditor();
					}
				});
		
		listComposite.addSelectionListener(
				new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event.getSelection();
						selectedAppComponent = (Component)selection.getFirstElement();
						showDetails();
						listComposite.removeButton.setEnabled(true);
					}
				});
		listSection.setClient(listComposite);
	    toolkit.paintBordersFor(listComposite);
	    toolkit.adapt(listComposite);
	}

	private void createGeneralComposite() {		
		Section generalSection = toolkit.createSection(this,ExpandableComposite.TITLE_BAR);
		generalSection.setText("Dynamic Application Platform");
		GridData gData = new GridData(SWT.FILL, SWT.FILL, true, true);
		gData.verticalSpan = 2;
		generalSection.setLayoutData(gData);
		GridLayout layout = new GridLayout();
		layout.marginWidth = 0;
		generalSection.setLayout(layout);
	}
	
	private void showDetails(){
		if (selectedAppComponent != null){
			nameText.setText(selectedAppComponent.getName());
			idText.setText(selectedAppComponent.getName());
			versionText.setText(selectedAppComponent.getVersion());
			locationText.setText(selectedAppComponent.getUrl());
			if (selectedAppComponent.getActions() != null && selectedAppComponent.getActions().getAction() != null)
				if (selectedAppComponent.getActions().getAction().contains(DeploymentConstants.Action.START))
					action.select(action.getItemCount()-1);
				else 
					action.select(0);
		}
	}
	
	public List getComponentList(){
		return listComposite.getInput();
	}
	

	public void markDirty() {
		((AbstractFormPart)mainPage.getManagedForm().getParts()[0]).markDirty();
	}
	
	public void markStale() {
		((AbstractFormPart)mainPage.getManagedForm().getParts()[0]).markStale();
	} 

}
